<?php

namespace app\common\business;

use app\common\lib\Num;
use app\common\lib\sms\AliSms;
use app\common\lib\sms\ClassArr;

class Sms
{
    public static function sendCode(string $phoneNumber, $len, $type = 'ali')
    {
        // 生成验证码
        $code = Num::getCode($len);
//        $sms = AliSms::sendCode($phoneNumber, $code);
        //工厂模式1
//        $type = ucfirst($type);
//        $ModelSms = "app\common\lib\sms\\".$type."Sms";
//        $sms = $ModelSms::sendCode($phoneNumber, $code);
        // 工厂模式2
        $classStats = ClassArr::smsClassStat();
        $clasObj = ClassArr::initClass($type, $classStats);
        $sms = $clasObj::sendCode($phoneNumber, $code);
        if ($sms) {
            // 记录redis
            cache(config("redis.code_pre").$phoneNumber, $code, config("redis.code_expire"));
        }
        return $sms;

    }
}